package cn.piggy.gateway.interfaces;

import cn.piggy.gateway.common.model.LoginUser;
import cn.piggy.gateway.common.util.JwtUtil;
import cn.piggy.gateway.rpc.ILoginInterface;
import cn.piggy.gateway.sdk.annotation.ApiProducerClazz;
import cn.piggy.gateway.sdk.annotation.ApiProducerMethod;
import com.alibaba.fastjson.JSONObject;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service(version = "1.0.0")
@Component
@ApiProducerClazz(interfaceName = "DUBBO-服务", interfaceVersion = "1.0.0")
public class LoginInterface implements ILoginInterface {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    @ApiProducerMethod(methodType = "DUBBO", uri = "/login", httpCommandType = "GET", auth = 0)
    public String login() {
        String key = "token:"+UUID.randomUUID().toString().replace("-", "");
        Set<String> set = new HashSet<>();
        set.add("user");
        set.add("admin");
        LoginUser loginUser = new LoginUser();
        loginUser.setToken(key);
        loginUser.setRoles(set);
        loginUser.setUserid(666L);
        long l = System.currentTimeMillis();
        loginUser.setExpireTime(l+1000*60*60);
        loginUser.setUsername("piggy");
        String jsonString = JSONObject.toJSONString(loginUser);
        redisTemplate.opsForValue().set(key,jsonString,1, TimeUnit.HOURS);
        return JwtUtil.createToken(key);
    }
}
